package com.mbc.web.common.server.controller;


import java.util.Date;

import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mbc.web.common.shared.HttpStatus;
import com.mbc.common.entities.Role;
import com.mbc.common.entities.User;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.DataNotFoundException;
import com.mbc.common.exception.ForeignConflictException;
import com.mbc.common.exception.UnauthorizedAccessException;
import com.mbc.common.manager.BranchMgr;
import com.mbc.common.manager.RoleMgr;
import com.mbc.common.manager.UserMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.web.common.client.controller.WebAccessController;
import com.mbc.web.common.server.SessionConstants;
import com.mbc.web.common.shared.dto.BranchChangeLogDataListDTO;
import com.mbc.web.common.shared.dto.LevelAppItemListDTO;
import com.mbc.web.common.shared.dto.RoleDataListDTO;
import com.mbc.web.common.shared.dto.RoleListDTO;
import com.mbc.web.common.shared.dto.UserDataListDTO;
import com.mbc.web.common.shared.dto.UserViewLoadDTO;

public class WebAccessConrollerImpl extends RemoteServiceServlet implements
		WebAccessController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1929894580557640650L;

	private HttpSession getSession()
	{
		return getThreadLocalRequest().getSession(); 
	}
	
	@Override
	public int createUser(User user)  {
		user.setCreatedBy(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
		user.setCreatedDate(new Date());
		user.setUpdatedBy(user.getCreatedBy());
		user.setUpdatedDate(user.getCreatedDate());
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			l_UserMgr.createUser(user, getSession().getAttribute(SessionConstants.LONGIN_ID).toString());
		} catch (Exception e) {
			if(e instanceof DataDuplicationException)
				return HttpStatus.BAD_REQUEST;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int updateUser(User user)  {
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			user.setUpdatedBy(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			user.setUpdatedDate(new Date());
			l_UserMgr.updateUser(user, getSession().getAttribute(SessionConstants.LONGIN_ID).toString());
		} catch (Exception e) {
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int deleteUser(long userID)  {
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			l_UserMgr.deleteUser(userID);
		} catch (Exception e) {
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public UserDataListDTO getUserList(CriteriaParams cri)
			 {
		UserDataListDTO l_dto = new UserDataListDTO();
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			l_dto.setUserDataList(l_UserMgr.getUserList(cri));
		} catch (Exception e) {
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public int createRole(Role role) {
		try {
			role.setCreated_By(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			role.setCreated_Date(new Date());
			role.setUpdated_By(role.getCreated_By());
			role.setUpdated_Date(new Date());
			
			RoleMgr l_roleMgr = new RoleMgr(getSession().getId());
			l_roleMgr.createRole(role);
		} catch (Exception e) {
			if(e instanceof DataDuplicationException)
				return HttpStatus.BAD_REQUEST;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int updateRole(Role role) {
		try {
			role.setUpdated_By(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			role.setUpdated_Date(new Date());
			
			RoleMgr l_roleMgr = new RoleMgr(getSession().getId());
			l_roleMgr.updateRole(role);
		} catch (Exception e) {
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int deleteRole(long roleId) {
		try {
			RoleMgr l_roleMgr = new RoleMgr(getSession().getId());
			l_roleMgr.deleteRole(roleId);
		} catch (Exception e) {
			if(e instanceof ForeignConflictException)
				return HttpStatus.FORBIDDEN;
			else
				return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public Role findRole(long roleId) {
		try {
			RoleMgr l_roleMgr = new RoleMgr(getSession().getId());
			return l_roleMgr.findRole(roleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public RoleListDTO getAllRole() {
		RoleListDTO l_dto = new RoleListDTO();
		try {
			RoleMgr l_RoleMgr = new RoleMgr(getSession().getId());
			l_dto.setRoleList(l_RoleMgr.findAll());
		} catch (Exception e) {
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public RoleDataListDTO getRoleList(CriteriaParams cri) {
		RoleDataListDTO l_dto = new RoleDataListDTO();
		try {
			RoleMgr l_RoleMgr = new RoleMgr(getSession().getId());
			l_dto.setRoleDataList(l_RoleMgr.getRoleList(cri));
		} catch (Exception e) {
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public LevelAppItemListDTO getLevelAppItemByRoleId(long roleId) {
		LevelAppItemListDTO l_dto = new LevelAppItemListDTO();
		try {
			RoleMgr l_RoleMgr = new RoleMgr(getSession().getId());
			l_dto.setLvlAppItemList(l_RoleMgr.getLevelAppItemByRoleId(roleId));
		} catch (Exception e) {
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public User getUser(long userId) {
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			return l_UserMgr.getUser(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public UserViewLoadDTO getUserViewLoadData() {
		UserViewLoadDTO l_dto = new UserViewLoadDTO();
		try {
			RoleMgr l_RoleMgr = new RoleMgr(getSession().getId());
			BranchMgr l_OfficeMgr = new BranchMgr(getSession().getId());
			
			l_dto.setBranchList(l_OfficeMgr.getAllBranch());
			l_dto.setRoleList(l_RoleMgr.findAll());
		} catch (Exception e) {
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public int resetLoginPassword(String oldPassword, String newPassword) {
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			l_UserMgr.resetPassword(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()), oldPassword, newPassword);
		} catch (Exception e) {
			if(e instanceof UnauthorizedAccessException)
				return HttpStatus.UNAUTHORIZED;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int changeUserBranch(String loginId, long branchId) {
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			l_UserMgr.changeUserBranch(loginId, branchId, getSession().getAttribute(SessionConstants.LONGIN_ID).toString());
		} catch (Exception e) {
			if(e instanceof DataNotFoundException)
				return HttpStatus.NOT_FOUND;
			else{
				e.printStackTrace();
				return HttpStatus.INTERNAL_SERVER_ERROR;
			}
		}
		return HttpStatus.OK;
	}

	@Override
	public BranchChangeLogDataListDTO findUserBranchChangeLogs(
			CriteriaParams cri) {
		BranchChangeLogDataListDTO l_dto = new BranchChangeLogDataListDTO();
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			l_dto.setDataList(l_UserMgr.getBranchChangeLog(cri));
		} catch (Exception e) {
			if(e instanceof DataNotFoundException)
				l_dto.setHttpStatus(HttpStatus.NOT_FOUND);
			else{
				e.printStackTrace();
				l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
			}
		}
		return l_dto;
	}

	@Override
	public User findUserByLoginId(String loginId) {
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			return l_UserMgr.getUser(loginId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
